package cn.iocoder.yudao.module.system.dal.mysql.scoreassignment;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.scoreassignment.vo.ScoreAssignmentRulePageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.scoreassignment.ScoreAssignmentRuleDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 赋分规则配置 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ScoreAssignmentRuleMapper extends BaseMapperX<ScoreAssignmentRuleDO> {

    default PageResult<ScoreAssignmentRuleDO> selectPage(ScoreAssignmentRulePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ScoreAssignmentRuleDO>()
                .likeIfPresent(ScoreAssignmentRuleDO::getRuleName, reqVO.getRuleName())
                .eqIfPresent(ScoreAssignmentRuleDO::getSubjectName, reqVO.getSubjectName())
                .eqIfPresent(ScoreAssignmentRuleDO::getGradeLevel, reqVO.getGradeLevel())
                .eqIfPresent(ScoreAssignmentRuleDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ScoreAssignmentRuleDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ScoreAssignmentRuleDO::getId));
    }

    default List<ScoreAssignmentRuleDO> selectList(ScoreAssignmentRulePageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ScoreAssignmentRuleDO>()
                .likeIfPresent(ScoreAssignmentRuleDO::getRuleName, reqVO.getRuleName())
                .eqIfPresent(ScoreAssignmentRuleDO::getSubjectName, reqVO.getSubjectName())
                .eqIfPresent(ScoreAssignmentRuleDO::getGradeLevel, reqVO.getGradeLevel())
                .eqIfPresent(ScoreAssignmentRuleDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ScoreAssignmentRuleDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ScoreAssignmentRuleDO::getId));
    }

    /**
     * 根据科目和等级查询赋分规则
     *
     * @param subjectName 科目名称
     * @param gradeLevel 等级
     * @return 赋分规则
     */
    default ScoreAssignmentRuleDO selectBySubjectAndGrade(String subjectName, String gradeLevel) {
        return selectOne(new LambdaQueryWrapperX<ScoreAssignmentRuleDO>()
                .eq(ScoreAssignmentRuleDO::getSubjectName, subjectName)
                .eq(ScoreAssignmentRuleDO::getGradeLevel, gradeLevel)
                .eq(ScoreAssignmentRuleDO::getStatus, 1)
                .last("LIMIT 1"));
    }

    /**
     * 根据科目查询所有启用的赋分规则
     *
     * @param subjectName 科目名称
     * @return 赋分规则列表
     */
    default List<ScoreAssignmentRuleDO> selectEnabledBySubject(String subjectName) {
        return selectList(new LambdaQueryWrapperX<ScoreAssignmentRuleDO>()
                .eq(ScoreAssignmentRuleDO::getSubjectName, subjectName)
                .eq(ScoreAssignmentRuleDO::getStatus, 1)
                .orderByAsc(ScoreAssignmentRuleDO::getMinPercentage));
    }

    /**
     * 根据规则名称查询所有启用的赋分规则
     *
     * @param ruleName 规则名称
     * @return 赋分规则列表
     */
    default List<ScoreAssignmentRuleDO> selectEnabledByRuleName(String ruleName) {
        return selectList(new LambdaQueryWrapperX<ScoreAssignmentRuleDO>()
                .eq(ScoreAssignmentRuleDO::getRuleName, ruleName)
                .eq(ScoreAssignmentRuleDO::getStatus, 1)
                .orderByAsc(ScoreAssignmentRuleDO::getSubjectName)
                .orderByAsc(ScoreAssignmentRuleDO::getMinPercentage));
    }

    /**
     * 查询所有启用的赋分规则
     *
     * @return 赋分规则列表
     */
    default List<ScoreAssignmentRuleDO> selectAllEnabled() {
        return selectList(new LambdaQueryWrapperX<ScoreAssignmentRuleDO>()
                .eq(ScoreAssignmentRuleDO::getStatus, 1)
                .orderByAsc(ScoreAssignmentRuleDO::getSubjectName)
                .orderByAsc(ScoreAssignmentRuleDO::getMinPercentage));
    }

    /**
     * 根据科目和百分比查询对应的赋分规则
     *
     * @param subjectName 科目名称
     * @param percentage 百分比
     * @return 赋分规则
     */
    default ScoreAssignmentRuleDO selectBySubjectAndPercentage(String subjectName, Double percentage) {
        return selectOne(new LambdaQueryWrapperX<ScoreAssignmentRuleDO>()
                .eq(ScoreAssignmentRuleDO::getSubjectName, subjectName)
                .le(ScoreAssignmentRuleDO::getMinPercentage, percentage)
                .ge(ScoreAssignmentRuleDO::getMaxPercentage, percentage)
                .eq(ScoreAssignmentRuleDO::getStatus, 1)
                .last("LIMIT 1"));
    }

}